<script lang="ts" setup>
import { onUnmounted, ref } from "vue";
import { mobileRules, passwordRules, codeRules } from "@/utils/rules";
import { Toast, type FormInstance } from "vant";
import { getCode, loginByCode, loginByPassword } from "@/services/user";
import useStore from "@/stores";
import { useRoute, useRouter } from "vue-router";

const { user } = useStore();
const router = useRouter();
const route = useRoute();
// 表单数据
const mobile = ref("13230000001");
const password = ref("abc12345");
const agree = ref(true);
// 控制密码是否显示
const show = ref(false);
// 切换短信或者密码登录
const isPass = ref(false);
// 验证码
const code = ref("");
// 表单
const form = ref<FormInstance>();
// 倒计时
const time = ref(0);
// 定时器
let timeId: number;
// 发送验证码
const send = async () => {
  if (time.value > 0) return;
  await form.value?.validate("mobile");
  await getCode(mobile.value, "login");
  time.value = 60;
  timeId = window.setTimeout(() => {
    time.value--;
    if (time.value <= 0) window.clearInterval(timeId);
  }, 1000);
};
// 清空定时器
onUnmounted(() => {
  window.clearInterval(timeId);
});
// 登录
const login = async () => {
  if (!agree) return Toast("请勾选我已同意");
  const res = isPass
    ? await loginByPassword(mobile.value, password.value)
    : await loginByCode(mobile.value, code.value);
  user.setUser(res.data);
  router.push((route.query.returnUrl as string) || "/user");
  Toast.success("登录成功！");
};
</script>
<template>
  <cp-nav-bar
    right-text="注册"
    @click-right="$router.push('/register')"
  ></cp-nav-bar>
  <div class="login">
    <div class="login-head">
      <h3>{{ isPass ? "密码登录" : "短信验证码登录" }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ !isPass ? "密码登录" : "短信验证码登录" }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form @submit="login" autocomplete="off">
      <van-field
        name="mobile"
        placeholder="请输入手机号"
        :rules="mobileRules"
        v-model="mobile"
        type="tel"
      ></van-field>
      <van-field
        v-model="password"
        v-if="isPass"
        placeholder="请输入密码"
        :rules="passwordRules"
        :type="show ? 'text' : 'password'"
      >
        <template #button>
          <cp-icon
            class="Icon"
            @click="show = !show"
            :name="`login-eye-${show ? 'on' : 'off'}`"
          ></cp-icon>
        </template>
      </van-field>
      <van-field
        v-else
        v-model="code"
        name="code"
        :rules="codeRules"
        placeholder="短信验证码"
      >
        <template #button>
          <span class="btn-send" :class="{ active: time > 0 }" @click="send">
            {{ time > 0 ? `${time}s后再次发送` : "发送验证码" }}
          </span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round native-type="submit" type="primary"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- ... -->
  </div>
</template>

<style lang="scss" scoped>
.login {
  padding-top: 46px;
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  // 新增form样式
  .van-form {
    padding: 0 14px;
    .cp-cell {
      height: 52px;
      padding: 14px 16px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      .van-checkbox {
        a {
          color: var(--cp-primary);
          padding: 0 5px;
        }
      }
    }
  }
}
</style>
